package com.bawei.viewgroper;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;

/**
 * 时间：2021/11/24 14:56.
 * 类名：TagerViewGrop
 * 作用：ViewGroper
 * 作者:张羽_zy
 */
public class TagerViewGrop extends ViewGroup {
    public int childHoriezonSpace;
    public int childVerTicalSpace;
    public TagerViewGrop(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray value=context.obtainStyledAttributes(attrs,R.styleable.TabLayout);
        if (value!=null)
        {
            childHoriezonSpace = value
                    .getDimensionPixelOffset(R.styleable.TagerViewGrop_tagHorizontalspace,0);
            childVerTicalSpace = value
                    .getDimensionPixelOffset(R.styleable.TagerViewGrop_tagVerticalSpace,0);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //获得父容器为它设置的测量模式和大小
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
        
        //如果是上下文情况下，记录宽高
        int width=0;
        int height=0;
        //记录每一行的宽度，width不断获取最大值
        int lineWidth=0;
        //每一行的高度，累加
        int lineHeight=0;
        int count=getChildCount();//计算子view个数
        int left=getPaddingLeft();
        int top=getPaddingTop();
        
        //遍历每个子元素
        for (int i = 0; i < count; i++) {
            View child=getChildAt(i);
            if (child.getVisibility() == GONE)
                continue;
                //测量每一个child的宽和高
                measureChild(child,widthMeasureSpec,heightMeasureSpec);
                //得到child的lp
                MarginLayoutParams lp= (MarginLayoutParams) child.getLayoutParams();
                //当前子控件实际占据的宽度
                int childWidth = child.getMeasuredWidth()+lp.leftMargin+lp.rightMargin-childHoriezonSpace;
                //当前子控件实际占据的高度
                int childHeight=child.getMeasuredHeight()+lp.topMargin+lp.bottomMargin+childVerTicalSpace;
            /**
             * 如果当前的行宽，大于当前viewGroup，则累加height，然后开始换行
             */
            if (lineWidth + childWidth > sizeWidth - getPaddingRight()-getPaddingLeft())
            {
                width = Math.max(lineWidth,childWidth);//取最大值
                lineWidth=childWidth;//重新开始新行
                //叠加当前高度
                height +=lineHeight;
                //开始记录下一行的高度
                lineHeight = childHeight;
                child.setTag(new Location(left,top+height,
                        lineWidth+childWidth-childHoriezonSpace+left,
                        height+child.getMeasuredHeight()+top));
            }else 
            {
                //否则累加linewidth，LineHeight取最大高度
                child.setTag(new Location(lineWidth+left,
                        top+height,
                        lineWidth+childWidth-childHoriezonSpace+left,
                        height+child.getMeasuredHeight()+top));
                
                lineWidth +=childWidth;
                lineHeight = Math.max(lineHeight,childHeight);
            }
        }
        width = Math.max(width,lineWidth)+getPaddingLeft()+getPaddingRight();
        height +=lineHeight;
        sizeHeight +=getPaddingTop()+getPaddingBottom();
        height +=getPaddingTop()+getPaddingBottom();
        setMeasuredDimension(
                (modeWidth==MeasureSpec.EXACTLY) ? sizeWidth:width,
                (modeHeight==MeasureSpec.EXACTLY) ? sizeHeight :height
        );
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View child =getChildAt(i);
            if (child.getVisibility() == GONE)
                continue;
            Location location= (Location) child.getTag();
            child.layout(location.left,location.top,location.right,location.bottom);
        }
    }
    /**
     * 记录子控件的坐标
     */
    public class Location
    {
        public int left;
        public int top;
        public int right;
        public int bottom;

        public Location(int left, int top, int right, int bottom) {
            this.left = left;
            this.top = top;
            this.right = right;
            this.bottom = bottom;
        }
    }
}
